<template>
	<view class="goods-item">
		<!-- 商品左侧图片区 -->
		<view class="goods-item-left">
			<!-- 官方基础组件，选择框 -->
			<radio :checked="goods.goods_state" color="#ffd242" v-if="showRadio" @click="radioChangeHandler"></radio>
			<image :src="goods.goods_small_logo ||defaultPic" mode=""></image>
		</view>
		<!-- 商品右侧信息区 -->
		<view class="goods-item-right">
			<view class="goods-info">{{goods.goods_name}}</view>
			<view class="goods-price-box">
				<view class="goods-price">
					￥{{goods.goods_price|tofixed}}
				</view>
				<!-- 官方组件：显示商品数量最小值为1 -->
				<uni-number-box v-if="showNum" @change="numChangeHandler" :min="1" :max="999"
					:value="goods.goods_count"></uni-number-box>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: "my-goods",
		// 核心代码，定义peops属性用来接收外界传递到当前组件的数据
		props: {
			// 商品的信息对象
			goods: {
				type: Object,
				default: {},
			},
			showRadio: {
				type: Boolean,
				default: false,
			},
			showNum: {
				type: Boolean,
				default: false
			}
		},
		data() {
			return {
				defaultPic: 'https://img0.baidu.com/it/u=3444803971,2413689688&fm=253&fmt=auto&app=120&f=JPEG?w=550&h=500', //图片不存在时默认使用的图片
			};
		},
		methods: {
			// 处理选中状态
			radioChangeHandler() {
				this.$emit('radio-change', {
					goods_id: this.goods.goods_id,
					goods_state: !this.goods.goods_state,
				})
			},
			// 处理商品数量
			numChangeHandler(val) {
				console.log(val);
				this.$emit('num-change', {
					goods_id: this.goods.goods_id,
					goods_count: +val
				})
			}
		},
		filters: {
			tofixed(num) {
				return Number(num).toFixed(2)
			}
		}
	}
</script>

<style lang="scss">
	.goods-item {
		border-bottom: 1px solid grey;
		display: flex;
		justify-content: space-between;
		padding-left: 5px;

		.goods-item-left {
			display: flex;
			justify-content: space-between;
			align-items: center;

			image {
				display: block;
				width: 100px;
				height: 100px;
			}
		}

		.goods-item-right {
			width: calc(100% - 110px);
			padding: 5px 0;
			display: flex;
			flex-direction: column;
			justify-content: space-evenly;

			.goods-info {
				font-size: 12px;
				color: black;
			}

			.goods-price-box {
				color: #ebc03d;
				display: flex;
				align-items: center;
				justify-content: space-between;
			}
		}
	}
</style>
